<?php

class TreeNode
{
	protected $parent;
	protected $children;
	protected $data;
	protected $furthestLeafDist;
	protected $rootDist;
	protected $childrenByDepth;
	
	public function __construct($data)
	{
		$this->parent = null;
		$this->data = $data;
		$this->furthestLeafDist = 0;
		$this->rootDist = 0;
	}
	
	public function addChild($node)
	{
		$this->children[] = $node;
		$this->childrenByDepth[0][] = $node;
		if (is_array($node->getChildrenByDepth()))
		{
			$this->childrenByDepth = array_merge_recursive($this->childrenByDepth, $node->getChildrenByDepth());
		}
		$node->setParent($this);
		$node->setRootDist($this->rootDist + 1);
		$this->compareFurthestLeafDist($node);
	}
	
	public function remove($index)
	{
		$node = $this->children[$index];
		unset($this->children[$index]);
		$this->children = array_values($this->children);
		$this->resetFurthestLeafDist();
	}
	
	private function resetFurthestLeafDist()
	{
		$this->furthestLeafDist = 0;
		foreach($this->children as $node)
		{
			$this->compareFurthestLeafDist($node);
		}
	}
	
	private function compareFurthestLeafDist($node)
	{
		if ($node->getFurthestLeafDist() >= $this->furthestLeafDist)
		{
			$this->furthestLeafDist = $node->getFurthestLeafDist() + 1;
		}
	}
	
	public function getParent()
	{
		return $this->parent;
	}
	
	public function getChildren()
	{
		return $this->children;
	}
	
	public function getData()
	{
		return $this->data;
	}
	
	public function getRootDist()
	{
		return $this->rootDist;
	}
	
	public function getFurthestLeafDist()
	{
		return $this->furthestLeafDist;
	}
	
	public function getChildrenByDepth()
	{
		return $this->childrenByDepth;
	}
	
	public function setParent($parent)
	{
		$this->parent = $parent;
	}
	
	public function setChildren($children)
	{
		$this->children($children);
	}
	
	public function setRootDist($rootDist)
	{
		$this->rootDist = $rootDist;
	}
	
	public function setFurthestLeafDist($furthestLeafDist)
	{
		$this->furthestLeafDist = $furthestLeafDist;
	}
}

$row = new TreeNode(null);
$scoring = new TreeNode("Scoring");
$desiredScore = new TreeNode("Desired Score");

$desiredScore->addChild(new TreeNode("Low"));
$desiredScore->addChild(new TreeNode("Med"));
$desiredScore->addChild(new TreeNode("High"));

$scoring->addChild(new TreeNode("Score"));
$scoring->addChild($desiredScore);

$row->addChild(new TreeNode("Metric Category"));
$row->addChild($scoring);

$rows = $row->getChildrenByDepth();

/*print_r($rows);

print_r($desiredScore->getChildren());
echo $scoring->getData();*/


$doc = new DOMDocument("1.0");

function arr2HtmlHeader($rows, $docFrag)
{
	$table = $docFrag->createElement("table");
	foreach($rows as $row)
	{
		$tr = $docFrag->createElement("tr");
		foreach($row as $col)
		{
			$td = $docFrag->createElement("td");
			if ($col->getFurthestLeafDist() > 0)
			{
				$td->setAttribute("rowspan", $col->getFurthestLeafDist() + 1);
			}
			$txt = new DOMText($col->getData());
			$td->appendChild($txt);
			$tr->appendChild($td);
		}
		$table->appendChild($tr);
	}
	return $table;
}

$xml = arr2HtmlHeader($rows, $doc);
$doc->appendChild($xml);
$doc->formatOutput = true;
echo $doc->saveHTML();
//echo $xml;

class TableTree
{
	
}
?>